//
//  PieLoopProgressView.swift
//  FromWatchToCircle
//
//  Created by 张海峰 on 2018/6/1.
//  Copyright © 2018年 张海峰. All rights reserved.
//
/*尊重OC原创作者：高少东 https://github.com/gsdios/SDProgressView.git */
/*写该demo的目的：主要是想从时钟开始，一步步窥见项目中和圆有关的东西，从而学习巩固，加深理解。
 (项目不定时更新，所以最好从下面地址获取最新demo),感觉我这个demo对你有启发或者帮助，欢迎fork 感谢star
 https://github.com/FighterLightning/FromWatchToCircle.git
 */
import UIKit
class PieLoopProgressView: BaseProgressView {
    override func draw(_ rect: CGRect) {
        super.draw(rect)
        let ctx: CGContext = UIGraphicsGetCurrentContext()!
        // 进度环边框
        UIColor.blue.set()// 背景颜色
        let radius :CGFloat = (defaultWH - ProgressViewItemMargin)/2 //半径
        let WH: CGFloat  = radius * 2 + 1 
        let x: CGFloat  = (defaultWH - WH)/2
        let y: CGFloat = (defaultWH - WH)/2
        ctx.addEllipse(in: CGRect.init(x: x, y: y, width: WH, height: WH))
        ctx.fillPath()
        //画进度条
        let centerXY: CGFloat = defaultWH/2
        UIColor.red.set()// 进度条颜色
        ctx.move(to: CGPoint.init(x: centerXY, y: centerXY))
        ctx.addLine(to: CGPoint.init(x: centerXY, y: 0))
       let endAngle: CGFloat = self.progress * .pi*2 + CGFloat(-Double.pi/2) + 0.05 ; // 初始值0.05
        ctx.addArc(center: CGPoint.init(x: centerXY, y: centerXY), radius: radius, startAngle: -.pi/2, endAngle: endAngle, clockwise: true)
        ctx.closePath()  //闭合路径，把起始点和终点连接起来
        ctx.fillPath()//实心
        //遮罩
        UIColor.yellow.set()
        let maskW: CGFloat = (radius - 15) * 2
        let maskH: CGFloat = maskW
        let maskX: CGFloat = (defaultWH - maskW)/2
        let maskY: CGFloat = (defaultWH - maskH)/2
        ctx.addEllipse(in: CGRect.init(x: maskX, y: maskY, width: maskW, height: maskH))
         ctx.fillPath()//实心
        // 遮罩边框
        UIColor.gray.set()
        let borderW: CGFloat = maskW + 1
        let borderH: CGFloat = borderW
        let borderX: CGFloat = (defaultWH - borderW)/2
        let borderY: CGFloat = (defaultWH - borderH)/2
        ctx.addEllipse(in: CGRect.init(x: borderX, y: borderY, width: borderW, height: borderH))
        ctx.strokePath() //空心 
        //显示进度数字
        //要绘制的文字
        let progressStr = "\(self.progress * 100)%"
        //文字样式属性
        let style = NSMutableParagraphStyle()
        style.alignment = .center
        let attributes = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: defaultTextFont),
                          NSAttributedStringKey.foregroundColor: UIColor.lightGray,
                          NSAttributedStringKey.paragraphStyle: style]
        self.setCenterProgressText(text: progressStr, attributes: attributes)
    }
}
